package org.huoran.sakamichi.main.util;

import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpUtil {

    public static String doPost(String url,Map<String,Object> paramMap){
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity requestEntity = new MultipartEntity();
        //装载参数
        Set<String> keySet = paramMap.keySet();
        for(String key : keySet){
            Object value = paramMap.get(key);
            if(value instanceof String){
                try {
                    requestEntity.addPart(key, new StringBody((String) value));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            else if(value instanceof File){
                requestEntity.addPart(key, new FileBody((File) value));
            }
            else if(value instanceof byte[]){
                requestEntity.addPart(key, new ByteArrayBody((byte[]) value,""));
            }
        }
        httpPost.setEntity(requestEntity);
        //发送请求
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将返回值转为字符串
        InputStream resultStream = null;
        try {
            resultStream = httpResponse.getEntity().getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String resultStr = inputStreamToString(resultStream);
        return resultStr;
    }

    private static String inputStreamToString(InputStream is) {
        if (is == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String result = sb.toString();
            return result;
        } catch (IOException e) {
            return "";
        }
    }

    public static void main(String[] args) throws IOException {
        Map<String,Object> paramMap = new HashMap<String, Object>();
        //读入图片
        ByteSource byteSource = Files.asByteSource(new File("C:\\Users\\lenovo\\Desktop\\testImage.jpg"));
        byte[] imageBytes = byteSource.read();
        paramMap.put("image", imageBytes);
        paramMap.put("api_key", "nJZ7hNoeNPBwWy541cqLKt5AvMqO_CiK");
        paramMap.put("api_secret", "K4IJliRwm6rvekLqPnf-z0VwCfTYRpoB");
        String responseStr = HttpUtil.doPost("https://api.megvii.com/faceid/v2/ocr_vehicle_license",paramMap);
//        String responseStr = HttpUtil.doPost("https://api.faceid.com/faceid/v1/ocridcard",paramMap);
        System.out.println(responseStr);
    }
}
